import {JsonObject, JsonProperty} from 'json2typescript';
import {Future} from '../util/future';

@JsonObject('ServiceEntity')
export class ServiceEntity {
  @JsonProperty()
  ID: string = undefined;
  @JsonProperty()
  Service: string = undefined;
  @JsonProperty()
  Tags: string[] = undefined;
  @JsonProperty()
  Meta: object = undefined;
  @JsonProperty()
  Port: number = undefined;
  @JsonProperty()
  Address: string = undefined;
  @JsonProperty()
  TaggedAddresses: object = undefined;
  @JsonProperty()
  Weights: object = undefined;
  @JsonProperty()
  EnableTagOverride: boolean = undefined;

  // tslint:disable-next-line:variable-name
  private _healthCheckTask: Future<string>;

  get healthCheck() {
    return this._healthCheckTask;
  }

  set healthCheck(v) {
    if (!this._healthCheckTask) {
      this._healthCheckTask = v;
    }
  }

  get state() {
    if (this.healthCheck) {
      if (this.healthCheck.isError) {
        return 'DOWN';
      }
      if (this.healthCheck.isDone) {
        return this.healthCheck.value;
      }
    }
    return null;
  }
}
